import logging

_inited = False  # 初始化标志


def init_logger(filename=None, fmt=None):
    global _inited
    if _inited:
        return

    if not fmt:
        fmt = '%(levelname)-5s [%(asctime)s] [%(thread)d] [%(filename)s=>%(funcName)s():%(lineno)d] - %(message)s'

    date_fmt = '%Y-%m-%d %H:%M:%S'

    logging.basicConfig(level=logging.INFO, filename=filename, datefmt=date_fmt, format=fmt)

    if filename:
        formatter = logging.Formatter(fmt, date_fmt)
        logger = logging.getLogger()

        chlr = logging.StreamHandler()  # 输出到控制台的handler
        chlr.setFormatter(formatter)
        chlr.setLevel('INFO')  # 也可以不设置，不设置就默认用logger的level
        logger.addHandler(chlr)
        #
        # fhlr = logging.FileHandler(filename)  # 输出到文件的handler
        # fhlr.setFormatter(formatter)
        # logger.addHandler(fhlr)

    _inited = True
